﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using wUniApp.MVC.App_Data;
using wUniApp.MVC.Helpers;

namespace wUniApp.MVC.Controllers
{
  public class wBaseController<T> : Controller where T : class
  {
    public wBaseController()
    {
      dbc = new wDbContext<T>();
      wLangHelper.Refresh();
    }

    protected override void Dispose(bool disposing)
    {
      if ((disposing) && (dbc != null))
      {
        dbc.Dispose();
        dbc = null;
      }
      base.Dispose(disposing);
    }

    protected wDbContext<T> dbc { get; private set; }

    // GET: T
    public virtual ActionResult Index()
    {
      return View(dbc.GetDbSet().ToList());
    }

    // GET: T/Details/5
    public ActionResult Details(int? id)
    {
      if (id == null)
      {
        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
      }
      T product = dbc.GetDbSet().Find(id);
      if (product == null)
      {
        return HttpNotFound();
      }
      return View(product);
    }

    // GET: T/Create
    public virtual ActionResult Create()
    {
      return View();
    }

    // POST: Products/Create
    // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
    // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
    [HttpPost]
    [ValidateAntiForgeryToken]
    public virtual ActionResult Create(T item)
    {
      if (ModelState.IsValid)
      {
        if (!dbc.Create(item))
        {
          return RedirectToProblem();
        }
        else
          return RedirectToAction("Index");
      }
      return View(item);
    }

    // GET: T/Edit/5
    public virtual ActionResult Edit(int? id)
    {
      if (id == null)
      {
        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
      }
      T item = dbc.Find((int)id);
      if (item == null)
      {
        return HttpNotFound();
      }
      return View(item);
    }



    // POST: T/Edit/5
    // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
    // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
    [HttpPost]
    [ValidateAntiForgeryToken]
    public virtual ActionResult Edit(T item)
    {
      if (ModelState.IsValid)
      {
        if (!dbc.Update(item))
        {
          return RedirectToProblem();
        }
        else
        return RedirectToAction("Index");
      }
      return View(item);
    }

    // GET: T/Delete/5
    public ActionResult Delete(int? id)
    {
      if (id == null)
      {
        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
      }
      T item = dbc.Find((int)id);
      if (item == null)
      {
        return HttpNotFound();
      }
      return View(item);
    }

    // POST: Products/Delete/5
    [HttpPost, ActionName("Delete")]
    [ValidateAntiForgeryToken]
    public ActionResult DeleteConfirmed(int id)
    {
      if (!dbc.Delete((int)id))
      {
        return RedirectToProblem();
      }
      else
      return RedirectToAction("Index");
    }

    private ActionResult RedirectToProblem()
    {
      string actionName = this.ControllerContext.RouteData.Values["action"].ToString();
      string controllerName = this.ControllerContext.RouteData.Values["controller"].ToString();
      var e = new System.Web.Mvc.HandleErrorInfo(dbc.LastException, controllerName, actionName);
      return View("Problem", e);
    }

  }
}